<?php
/**
 * Created by PhpStorm.
 * User: lelouchms
 * Date: 2019/6/3
 * Time: 10:46
 */

namespace app\index\controller;


use app\common\exception\CustomerException;
use app\common\model\UserComplaint;
use app\common\util\CommonUtil;

class User extends Base
{

    public function initialize()
    {
        parent::initialize();
    }

    /**
     * 用户信息
     * @return array|mixed|null|\PDOStatement|string|\think\Model
     */
    public function detail()
    {
        $model = new \app\common\model\User();
        $userInfo = $model->findById(session("web_user")['id']);
        unset($userInfo['password']);
        unset($userInfo['open_id']);
        if (!empty($userInfo['avatar'])) CommonUtil::formatHostUrl($userInfo['avatar']);
        return $userInfo;
    }

    /**
     * 绑定手机
     * @return string
     * @throws CustomerException
     */
    public function bindPhone()
    {
        $requestData = $this->request->only(["phone", "code"]);
        if (empty($requestData['code']) || $requestData['code'] !== "666666") throw new CustomerException("验证码错误");
        $webUser = session("web_user");
        $model = new \app\common\model\User();
        $findUser = $model->findByPhone($requestData['phone']);
        if (!empty($findUser)) throw new CustomerException("手机号码已被注册");
        $count = $model->where("id", $webUser['id'])->update(["phone" => $requestData["phone"]]);
        if ($count > 1) throw new CustomerException("绑定失败");
        $webUser = $model->findById($webUser['id']);
        session("web_user", $webUser);
        return "绑定成功";
    }

    /**
     * 投诉建议
     * @return string
     * @throws CustomerException
     */
    public function userComplaint()
    {
        $content = $this->request->post("content");
        if (empty($content)) throw new CustomerException("请输入您的意见和建议");
        (new UserComplaint())->save([
            "content" => $content,
            "user_id" => session("web_user")["id"],
            "status" => 1
        ]);
        return "感谢您提供的意见和建议";
    }

}